﻿using QL_DTTN.DAL;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace QL_DTTN.DTO.DTO
{
    public partial class Quyen
    {
        public Quyen GetQuyen(short maQuyen)
        {
            bool kt = false;
            SqlDataProvider dp = new SqlDataProvider();
            System.Data.SqlClient.SqlDataReader sqlRr = GetObject(maQuyen, dp);
            Quyen tk = new Quyen();
            if (sqlRr != null)
            {
                while (sqlRr.Read())
                {
                    if (!Convert.IsDBNull(sqlRr["TenQuyen"])) tk.TenQuyen = sqlRr["TenQuyen"].ToString();
                    if (!Convert.IsDBNull(sqlRr["MaQuyen"])) tk.MaQuyen = Convert.ToInt16(sqlRr["MaQuyen"]);
                    if (!Convert.IsDBNull(sqlRr["Khoa"])) tk.Khoa = Convert.ToBoolean(sqlRr["Khoa"]);
                    kt = true;
                }
                dp.CloseConnection();
                if (kt) return tk;
                else return null;
            }
            else
            {
                return null;
            }
        }
        public List<ChucNang> lstChucNang(short maQuyen)
        {
            bool kt = false;
            string sql = " select distinct CN.MaChucNang,CN.TenChucNang from ChiTietChucNang C ";
            sql += " inner join ChucNang CN on C.MaChucNang = CN.MaChucNang ";
            sql += " inner join ChiTietQuyen Q on Q.MaChiTietChucNang = C.MaChiTietChucNang ";
            sql += " where Q.MaQuyen=@pMaQuyen ";
            SqlDataProvider dp = new SqlDataProvider();
            dp.AddParameter(new SqlParameter("@pMaQuyen", maQuyen));
            System.Data.SqlClient.SqlDataReader sqlRr = dp.ExDataReader(sql, true);
            List<ChucNang> lstCN = new List<ChucNang>();
            if (sqlRr != null)
            {
                while (sqlRr.Read())
                {
                    ChucNang cn = new ChucNang();
                    if (!Convert.IsDBNull(sqlRr["MaChucNang"])) cn.MaChucNang = sqlRr["MaChucNang"].ToString();
                    if (!Convert.IsDBNull(sqlRr["TenChucNang"])) cn.TenChucNang = sqlRr["TenChucNang"].ToString();
                    kt = true;
                    lstCN.Add(cn);
                }
                dp.CloseConnection();
                if (kt) return lstCN;
                else return new List<ChucNang>();
            }
            else
            {
                return new List<ChucNang>();
            }
        }
        public List<CongCu> lstCongCu(short maQuyen, string maChucNang)
        {
            bool kt = false;
            string sql = " select distinct CC.MaCongCu,CC.TenCongCu  from ChiTietChucNang C ";
            sql += " inner join CongCu CC on CC.maCongCu=C.maCongCu ";
            sql += " inner join ChiTietQuyen Q on Q.MaChiTietChucNang = C.MaChiTietChucNang ";
            sql += " where Q.MaQuyen=@pMaQuyen and C.MaChucNang=@pMaChucNang ";
            SqlDataProvider dp = new SqlDataProvider();
            dp.AddParameter(new SqlParameter("@pMaQuyen", maQuyen));
            dp.AddParameter(new SqlParameter("@pMaChucNang", maChucNang));
            System.Data.SqlClient.SqlDataReader sqlRr = dp.ExDataReader(sql, true);
            List<CongCu> lstCC = new List<CongCu>();
            if (sqlRr != null)
            {
                while (sqlRr.Read())
                {
                    CongCu cc = new CongCu();
                    if (!Convert.IsDBNull(sqlRr["MaCongCu"])) cc.MaCongCu = Convert.ToInt16(sqlRr["MaCongCu"]);
                    if (!Convert.IsDBNull(sqlRr["TenCongCu"])) cc.TenCongCu = sqlRr["TenCongCu"].ToString();
                    kt = true;
                    lstCC.Add(cc);
                }
                dp.CloseConnection();
                if (kt) return lstCC;
                else return new List<CongCu>();
            }
            else
            {
                return new List<CongCu>();
            }
        }
    }
}
